#include <iostream>
#include <cstdio>
#include <algorithm> 

using namespace std; 

#define N 1000001

struct node_s {
	node_s *left, *right; 
	int id; 
	int val; 
	int sum; 
	int nr_node; 
}; 

node_s nd[N];
int n; 

void traval(node_s *root)
{
	root->sum = root->val; 
	root->nr_node = 1; 
	if (root->left) {
		traval(root->left); 
		root->sum += root->left->sum; 
		root->nr_node += root->left->nr_node; 
	}
	if (root->right) {
		traval(root->right); 
		root->sum += root->right->sum; 
		root->nr_node += root->right->nr_node; 
	}
}

void spc(int lvl)
{
	int i; 
	for (i=0; i<lvl; i++) {
		printf("    "); 
	}
}

void dump(int lvl, node_s *p)
{
	if (p==NULL)
		return; 
	spc(lvl); 
	printf("id=%d, nr_node=%d, val=%d, sum=%d\n", 
			p->id, p->nr_node, p->val, p->sum); 
	if (p->left==NULL && p->right==NULL)
		return; 
	if (p->left) {
		spc(lvl); 
		printf("    %d->left: \n", p->id); 
		dump(lvl+1, p->left); 
	}
	if (p->right) {
		spc(lvl); 
		printf("    %d->right: \n", p->id); 
		dump(lvl+1, p->right); 
	}
}


int equal(node_s *a, node_s *b)
{
	if (a==NULL && b==NULL) {
		return 1; 
	}
	
	if ((a!=NULL&&b==NULL) || (a==NULL&&b!=NULL)) {
		return 0; 
	}

	if (a->nr_node!=b->nr_node) {
		return 0; 
	}
	
	if (a->sum!=b->sum || a->val!=b->val) {
		return 0; 
	}
	
	return equal(a->left, b->right) && equal(a->right, b->left); 
}

int is_symmetric(node_s *root)
{
	if (root->nr_node==1) 
		return 1; 
	if ((root->left==NULL && root->right!=NULL) 
		|| (root->left!=NULL && root->right==NULL))
		return 0; 
	return equal(root->left, root->right); 
}

int traval_1(void)
{
	int i, ans=0; 
	
	for (i=1; i<=n; i++) {
		if (is_symmetric(nd+i)) {
        	//printf("%d: %d\n", i, nd[i].nr_node); 
			ans = max(ans, nd[i].nr_node); 
		} 
	}

	return ans; 
}

int main()
{
	int i, l, r; 
	
	//freopen("p5018_5.in", "r", stdin); 
	
	cin>>n; 
	for (i=1; i<=n; i++) {
		cin>>nd[i].val; 
		nd[i].id = i; 
		nd[i].sum = nd[i].nr_node = 0; 
		nd[i].left = nd[i].right = NULL; 
	}
	
	for (i=1; i<=n; i++) {
		cin>>l>>r; 
		if (l!=-1)
			nd[i].left = nd+l; 
		if (r!=-1)
			nd[i].right = nd+r; 
	}

	traval(nd+1); 
	//dump(0, &nd[65826]); 
	i = traval_1(); 
	printf("%d\n", i); 
	return 0; 
}



